/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bc5Neptune.cis.gui;

import com.bc5Neptune.cis.config.ConnectDB2;
import com.bc5Neptune.cis.dal.Employee;
import com.nilo.plaf.nimrod.NimRODLookAndFeel;
import com.nilo.plaf.nimrod.NimRODTheme;
import java.awt.Container;
import java.sql.*;
import javax.swing.JFrame;
import javax.swing.UIManager;
import static com.bc5Neptune.cis.bll.GlobalObject.*;

/**
 *
 * @author huynhtanphu
 */
public class GLogin extends javax.swing.JFrame {

    public static String userNameAccess;
    /* 1: administratro, 2 employee */
    public static int role = 2;//employee
    /**
     * Creates new form GLogin
     */
    ConnectDB2 obj = new ConnectDB2();

    public GLogin() {
        initComponents();
        obj.getConnection();
        //set align center screen
        Container c = new Container();
        this.setLocationRelativeTo(c);
    }

    public void checkLogin() {
        lblcheckuser.setText(null);
        lblcheckpass.setText(null);

        String username = txfusername.getText();
        String pass = txfpass.getText();

        String dbUsername = null;
        String dbPass = null;
        Employee obj2 = new Employee();
        try {

            dbUsername = obj2.checkLog(username, pass).getUsername();

            dbPass = obj2.checkLog(username, pass).getPassword();

            System.out.println("Db Name" + dbUsername);
            System.out.println("Db Pass" + dbPass);
            if (dbPass != null && dbUsername != null) {

                GLogin.role = obj2.checkLog(dbUsername, dbPass).getRoleid();//get role
                System.out.println("roleid la: " + GLogin.role);
                GLogin.userNameAccess = dbUsername;//get username from database
                System.out.println("Login user name befor --" + GLogin.userNameAccess);
                //the impotant to run application
                //close login form and run application
                this.setVisible(false);
                new GApplication().setVisible(true);

                GLPCustom = new PFaceCustom();
                GLPReg = new PFaceRecognition();
                GLPDetect = new PFaceDetection();


                txfusername.setText(null);
                txfpass.setText(null);
            }
            if (dbUsername != null && dbPass == null) {
                txfpass.setText(null);
                lblcheckpass.setText("Password is incorrect!");
                return;
            }
            if (dbUsername == null && dbPass != null) {
                txfusername.setText(null);
                lblcheckuser.setText("Username is incorrect!");
                return;
            }
            if (username.isEmpty() == true) {
                lblcheckuser.setText("Please enter your username");
                return;
            }
            if (pass.isEmpty() == true) {
                lblcheckpass.setText("Please enter your password!");
                return;
            }
            if (pass.isEmpty() == true && username.isEmpty() == true) {
                lblcheckpass.setText("Please enter your password and username!");
                return;
            }
            if (dbPass == null && dbUsername == null) {
                txfpass.setText(null);
                txfusername.setText(null);
                lblcheckpass.setText("Password or username is incorrect!");
                return;

            }

        } catch (Exception e) {
            e.printStackTrace();

        }
    }

    private void formKeyPressed(java.awt.event.KeyEvent evt) {
// TODO add your handling code here:
    }

    private void close() {
        this.setVisible(false);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        txfusername = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        lblcheckuser = new javax.swing.JLabel();
        txfpass = new javax.swing.JPasswordField();
        lblcheckpass = new javax.swing.JLabel();
        btnSubmit = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setType(java.awt.Window.Type.POPUP);

        jPanel1.setBackground(java.awt.Color.gray);
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        txfusername.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txfusernameActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Abyssinica SIL", 0, 14)); // NOI18N
        jLabel2.setForeground(java.awt.Color.white);
        jLabel2.setText("Username");

        jLabel3.setFont(new java.awt.Font("Abyssinica SIL", 0, 14));
        jLabel3.setForeground(java.awt.Color.white);
        jLabel3.setText("Password");

        lblcheckuser.setFont(new java.awt.Font("Cantarell", 0, 14)); // NOI18N
        lblcheckuser.setForeground(java.awt.Color.white);

        txfpass.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txfpassActionPerformed(evt);
            }
        });

        lblcheckpass.setFont(new java.awt.Font("Cantarell", 0, 14)); // NOI18N
        lblcheckpass.setForeground(java.awt.Color.white);

        btnSubmit.setBackground(new java.awt.Color(255, 158, 0));
        btnSubmit.setFont(new java.awt.Font("Abyssinica SIL", 0, 15)); // NOI18N
        btnSubmit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/login.png"))); // NOI18N
        btnSubmit.setText("Login");
        btnSubmit.setName(""); // NOI18N
        btnSubmit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSubmitActionPerformed(evt);
            }
        });

        btnCancel.setFont(new java.awt.Font("Abyssinica SIL", 0, 15)); // NOI18N
        btnCancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/Cancel_16x16.png"))); // NOI18N
        btnCancel.setText("Cancel");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        jLabel5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/login/login.png"))); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(lblcheckuser, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addComponent(btnSubmit)
                                    .addGap(18, 18, 18)
                                    .addComponent(btnCancel))
                                .addComponent(lblcheckpass, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(txfpass, javax.swing.GroupLayout.DEFAULT_SIZE, 222, Short.MAX_VALUE)
                                .addComponent(txfusername, javax.swing.GroupLayout.PREFERRED_SIZE, 223, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(74, 74, 74)
                        .addComponent(jLabel5)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addGap(12, 12, 12)
                .addComponent(txfusername, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblcheckuser, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(4, 4, 4)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txfpass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblcheckpass, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSubmit)
                    .addComponent(btnCancel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        jPanel1Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {btnCancel, btnSubmit});

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txfusernameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txfusernameActionPerformed
// TODO add your handling code here:    
        btnSubmitActionPerformed(evt);
    }//GEN-LAST:event_txfusernameActionPerformed

    private void txfpassActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txfpassActionPerformed
// TODO add your handling code here: 
        btnSubmitActionPerformed(evt);
    }//GEN-LAST:event_txfpassActionPerformed

    private void btnSubmitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSubmitActionPerformed
// TODO add your handling code here:
        checkLogin();
        // close();
        //new GApplication().setVisible(true);

    }//GEN-LAST:event_btnSubmitActionPerformed

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
// TODO add your handling code here:
        System.exit(0);
    }//GEN-LAST:event_btnCancelActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {

        try {
            UIManager.setLookAndFeel(new com.nilo.plaf.nimrod.NimRODLookAndFeel());
            final NimRODTheme nt = new NimRODTheme("../CIS_SProjectR2/lib/Snow.theme");
            final NimRODLookAndFeel nf = new NimRODLookAndFeel();
            NimRODLookAndFeel.setCurrentTheme(nt);
            UIManager.setLookAndFeel(nf);
        } catch (final Exception ex) {
            ex.printStackTrace();
        }
//        /* Create and display the form */
        JFrame.setDefaultLookAndFeelDecorated(true);
        /*
         * Create and display the form
         */

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new GLogin().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnSubmit;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel lblcheckpass;
    private javax.swing.JLabel lblcheckuser;
    private javax.swing.JPasswordField txfpass;
    private javax.swing.JTextField txfusername;
    // End of variables declaration//GEN-END:variables
}
